package com.gateway.util;

import org.apache.log4j.Logger;
import org.hyperic.sigar.CpuInfo;
import org.hyperic.sigar.CpuPerc;
import org.hyperic.sigar.Mem;
import org.hyperic.sigar.NetInterfaceConfig;
import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.SigarException;
import org.hyperic.sigar.Swap;

/**
 * Sigar工具类
 * @author Simple
 * @date 2013-7-12 下午03:05:08
 * @Description TODO
 */
public class SigarTool {

  private static Logger log=Logger.getLogger(SigarTool.class);

  public static void outMemInfo(Sigar sigar) throws SigarException {
    Mem mem=sigar.getMem();
    log.debug("[Mem INFO]");
    log.debug(String.format("ActualFree..... %s MB", MathTool.calc(mem.getActualFree() / 1024.0 / 1024.0)));
    log.debug(String.format("ActualUsed..... %s MB", MathTool.calc(mem.getActualUsed() / 1024.0 / 1024.0)));
    log.debug(String.format("Free........... %s MB", MathTool.calc(mem.getFree() / 1024.0 / 1024.0)));
    log.debug(String.format("FreePercent.... %s", MathTool.calc(mem.getFreePercent())) + "%");
    log.debug(String.format("Ram............ %s", mem.getRam()));
    log.debug(String.format("Total.......... %s MB", MathTool.calc(mem.getTotal() / 1024.0 / 1024.0)));
    log.debug(String.format("Used........... %s MB", MathTool.calc(mem.getUsed() / 1024.0 / 1024.0)));
    log.debug(String.format("UsedPercent.... %s", MathTool.calc(mem.getUsedPercent())) + "%");
  }

  public static void outSwapInfo(Sigar sigar) throws SigarException {
    Swap swap=sigar.getSwap();
    log.debug("[Swap INFO]");
    log.debug(String.format("Free........... %s MB", MathTool.calc(swap.getFree() / 1024.0 / 1024.0)));
    log.debug(String.format("PageIn......... %s MB", MathTool.calc(swap.getPageIn() / 1024.0 / 1024.0)));
    log.debug(String.format("PageOut........ %s MB", MathTool.calc(swap.getPageOut() / 1024.0 / 1024.0)));
    log.debug(String.format("Total.......... %s MB", MathTool.calc(swap.getTotal() / 1024.0 / 1024.0)));
    log.debug(String.format("Used........... %s MB", MathTool.calc(swap.getUsed() / 1024.0 / 1024.0)));
  }

  public static void outCpuInfo(Sigar sigar) throws SigarException {
    CpuInfo[] cpuInfos=sigar.getCpuInfoList();
    if(cpuInfos.length > 0) {
      CpuInfo cpuInfo=cpuInfos[0];
      log.debug("[Cpu INFO]");
      log.debug(String.format("Vendor........... %s", cpuInfo.getVendor()));
      log.debug(String.format("Model............ %s", cpuInfo.getModel()));
      log.debug(String.format("Mhz.............. %s GHz", cpuInfo.getMhz()));
      log.debug(String.format("TotalCores....... %s核", cpuInfo.getTotalCores()));
      log.debug(String.format("TotalSockets..... %s", cpuInfo.getTotalSockets()));
      log.debug(String.format("CacheSize........ %s", cpuInfo.getCacheSize()));
      log.debug(String.format("CoresPerSocket... %s", cpuInfo.getCoresPerSocket()));
    }
  }

  public static void outCpuPerc(Sigar sigar) throws SigarException {
    CpuPerc[] cpuPercs=sigar.getCpuPercList();
    int i=0;
    for(CpuPerc perc: cpuPercs) {
      log.debug("[Cpu " + i + "]");
      log.debug(String.format("User Time..... %s", MathTool.calc(perc.getUser() * 100)) + "%");
      log.debug(String.format("Idle Time..... %s", MathTool.calc(perc.getIdle() * 100)) + "%");
      log.debug(String.format("Wait Time..... %s", MathTool.calc(perc.getWait() * 100)) + "%");
      log.debug(String.format("Nice Time..... %s", MathTool.calc(perc.getNice() * 100)) + "%");
      log.debug(String.format("Sys  Time..... %s", MathTool.calc(perc.getSys() * 100)) + "%");
      log.debug(String.format("Irq  Time..... %s", MathTool.calc(perc.getIrq() * 100)) + "%");
      log.debug(String.format("Combined...... %s", MathTool.calc(perc.getCombined() * 100)) + "%");
      i++;
    }
  }

  public static void outNetInfo(Sigar sigar) throws SigarException {
    log.debug("[Net INFO]");
    NetInterfaceConfig net=sigar.getNetInterfaceConfig();
    log.debug(String.format("NAME...... %s", net.getName()));
    log.debug(String.format("IP........ %s", net.getAddress()));
    log.debug(String.format("MAC....... %s", net.getHwaddr()));
  }
}
